package com.thb.pay.service.impl;

import com.thb.pay.core.utils.Constant;
import com.thb.pay.db.service.impl.BaseServiceImpl;
import com.thb.pay.entity.ScheduleJobEntity;
import com.thb.pay.mapper.ScheduleJobMapper;
import com.thb.pay.service.ScheduleJobService;
import com.thb.pay.utils.utils.ScheduleUtils;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("scheduleJobService")
public class ScheduleJobServiceImpl extends BaseServiceImpl<ScheduleJobMapper, ScheduleJobEntity> implements ScheduleJobService {
    @Autowired
    private Scheduler scheduler;
    @Autowired
    private ScheduleJobMapper scheduleJobMapper;

    /**
     * 项目启动时，初始化定时器
     */
    @PostConstruct
    public void init() {
        List<ScheduleJobEntity> scheduleJobList = scheduleJobMapper.selectList(null);
        if (CollectionUtils.isEmpty(scheduleJobList)) {
            return;
        }
        for (ScheduleJobEntity scheduleJob : scheduleJobList) {
            CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, scheduleJob.getId());
            //如果不存在，则创建
            if (cronTrigger == null) {
                ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
            } else {
                ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
            }
        }
    }

    @Override
    public ScheduleJobEntity queryObject(Long jobId) {
        return scheduleJobMapper.selectById(jobId);
    }

    @Override
    public List<ScheduleJobEntity> queryList(Map<String, Object> map) {
        return scheduleJobMapper.selectByMap(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        List<ScheduleJobEntity> scheduleJobEntityList = this.queryList(map);
        if (CollectionUtils.isEmpty(scheduleJobEntityList)) {
            return 0;
        }
        return scheduleJobEntityList.size();
    }

    @Transactional
    public boolean save(ScheduleJobEntity scheduleJob) {

        scheduleJob.setStatus(Constant.ScheduleStatus.NORMAL.getValue());
        super.save(scheduleJob);

        ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
        return true;
    }

    @Override
    public void update(ScheduleJobEntity scheduleJob) {
        ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
        scheduleJob.setUpdateTime(new Date());
        super.updateById(scheduleJob);
    }

    @Override
    public void deleteBatch(Long[] jobIds) {
        for (Long jobId : jobIds) {
            ScheduleUtils.deleteScheduleJob(scheduler, jobId);
        }
        //删除数据
        super.deleteBatchIds(CollectionUtils.arrayToList(jobIds));
    }

    @Override
    public int updateBatch(Long[] jobIds, int status) {
        Map<String, Object> map = new HashMap<>();
        map.put("list", jobIds);
        map.put("status", status);
        return scheduleJobMapper.updateBatch(map);
    }

    @Override
    public void run(Long[] jobIds) {
        for (Long jobId : jobIds) {
            ScheduleUtils.run(scheduler, queryObject(jobId));
        }
    }

    @Override
    public void pause(Long[] jobIds) {
        for (Long jobId : jobIds) {
            ScheduleUtils.pauseJob(scheduler, jobId);
        }

        updateBatch(jobIds, Constant.ScheduleStatus.PAUSE.getValue());
    }

    @Override
    public void resume(Long[] jobIds) {
        for (Long jobId : jobIds) {
            ScheduleUtils.resumeJob(scheduler, jobId);
        }

        updateBatch(jobIds, Constant.ScheduleStatus.NORMAL.getValue());
    }

}
